package com.hliushi.redis.controller;

import com.hliushi.redis.config.Constants;
import com.hliushi.redis.controller.vo.ResResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author llhuang10
 * @date 2021/8/14 11:32
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class JdDrawController {


    @Resource
    private RedisTemplate redisTemplate;

    /**
     * d
     *
     * @param
     * @return
     */
    @GetMapping("/jd-draw")
    public ResponseEntity<ResResult> getJd() {
        ResResult resResult = new ResResult();
        try {
            // 随机取一次
            String randomMember = (String) redisTemplate.opsForSet().randomMember(Constants.PRICE_KEY);

            if (StringUtils.isNotEmpty(randomMember)) {
                int level = Integer.parseInt(randomMember.split("-")[0]);

                switch (level) {
                    case 10:
                        resResult.setMsg("获取10金豆");
                        resResult.setCode(10L);
                        break;
                    case 5:
                        resResult.setMsg("获取5金豆");
                        resResult.setCode(5L);
                        break;
                    case 1:
                        resResult.setMsg("获取1金豆");
                        resResult.setCode(1L);
                        break;
                    case 0:
                        resResult.setMsg("谢谢参与");
                        resResult.setCode(0L);
                        break;
                    default:
                        resResult.setMsg("谢谢参与");
                        resResult.setCode(0L);
                }
                log.info("查询结果: {}", randomMember);
                // todo 将个人的抽奖结果保存到DB
            }

        } catch (Exception e) {
            log.error("exception:", e);
        }

        return ResponseEntity.ok(resResult);
    }
}
